package cn.com.thingo.hyintelligent.ui.activity;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.RemoterInfo;
import com.th.opensdk.openapi.RemoterKeyInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.base.BaseActivity;
import cn.com.thingo.hyintelligent.base.contract.DoorLockControlContract;
import cn.com.thingo.hyintelligent.presenter.DoorLockControlPresenter;
import cn.com.thingo.hyintelligent.ui.adapter.DoorLockControlAdapter;

public class DoorLockControlActivity extends BaseActivity<DoorLockControlPresenter> implements DoorLockControlContract.View, DoorLockControlAdapter.CallBack {
    private THOpenSDK thOpenSDK;
    private RemoterInfo remoterInfo;
    private List<RemoterKeyInfo> remoterKeyList = new ArrayList<>();
    private DoorLockControlAdapter adapter;
    @BindView(R.id.toolbar_left)
    ImageView ivBack;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.door_lock_control_list)
    RecyclerView rvList;


    @Override
    protected int getLayout() {
        return R.layout.activity_door_lock_control;
    }

    @Override
    protected void initEventAndData() {
        thOpenSDK = THOpenSDK.getInstance();
        initView();
        initData();

    }

    private void initData() {
        long airConditionId = getIntent().getLongExtra("remoter_id", -1);
        List<RemoterInfo> remoterList = thOpenSDK.getRemoterList();
        for (int i = 0; i < remoterList.size(); i++) {
            if (remoterList.get(i).getRemoterId() == airConditionId) {
                remoterInfo = remoterList.get(i);
                break;
            }
        }
        Map<Integer, RemoterKeyInfo> keyList = remoterInfo.getKeyList();
        if (keyList != null && keyList.size() > 0) {
            remoterKeyList.clear();
            Set<Integer> integers = keyList.keySet();
            for (Integer next : integers) {
                RemoterKeyInfo remoterKeyInfo = keyList.get(next);
                remoterKeyList.add(remoterKeyInfo);
            }
            adapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        ivBack.setImageResource(R.mipmap.back);
        String remote_name = getIntent().getStringExtra("remote_name");
        tvTitle.setText(remote_name);
        adapter = new DoorLockControlAdapter(remoterKeyList, mContext);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter.setCallBack(this);
        rvList.setAdapter(adapter);

    }

    @OnClick({R.id.toolbar_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void callBack(int position) {
        RemoterKeyInfo remoterKeyInfo = remoterKeyList.get(position);
        remoterInfo.sendRemoterCode(remoterKeyInfo.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
    }
}
